/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
// 递归
class Solution {
private:
    vector<int> vec1;
    vector<int> vec2;
public:
    bool isSymmetric(TreeNode* root) {
        if(root == nullptr) return true;

        return compare(root, root);
    }

    bool compare(TreeNode* leftNode, TreeNode* rightNode){
        if(leftNode == nullptr && rightNode == nullptr) return true;
        if(leftNode == nullptr && rightNode != nullptr) return false;
        if(leftNode != nullptr && rightNode == nullptr) return false;

        if(leftNode->val != rightNode->val) return false;

        return compare(leftNode->left, rightNode->right) && compare(leftNode->right, rightNode->left);
    }
};

// 迭代
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        queue<TreeNode*> queue;

        if(root != nullptr) {
            queue.push(root);
            queue.push(root);
        }

        while(queue.empty() == false){
            TreeNode* leftNode = queue.front(); queue.pop();
            TreeNode* rightNode = queue.front(); queue.pop();

            if(leftNode->val != rightNode->val) return false;
            if((leftNode->left || rightNode->right) != (leftNode->left && rightNode->right)) return false;
            if((leftNode->right || rightNode->left) != (leftNode->right && rightNode->left)) return false;

            if(leftNode->left && rightNode->right){
                queue.push(leftNode->left);
                queue.push(rightNode->right);
            }

            if(leftNode->right && rightNode->left){
                queue.push(leftNode->right);
                queue.push(rightNode->left);
            }
        }

        return true;
    }
};